using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;

public class Head : MonoBehaviour
{
    [SerializeField]private GameObject node;
    [SerializeField]private FoodCreater foodCreater;
    private readonly List<GameObject> mBody = new List<GameObject>();
    private FMSLite mFMSLite;

    private class Direction
    {
        public const string UP = "Up";
        public const string DOWN = "Down";
        public const string LEFT = "Left";
        public const string RIGHT = "Right";
    }

    private Vector2 mCurrentDirection = Vector2.up;

    private void Start()
    {
        AddFMSState();
    }

    private void AddFMSState()
    {
        mFMSLite = new FMSLite();
        mFMSLite.AddState(Direction.UP);
        mFMSLite.AddState(Direction.DOWN);
        mFMSLite.AddState(Direction.LEFT);
        mFMSLite.AddState(Direction.RIGHT);
        mFMSLite.AddTranslation(Direction.UP, Direction.LEFT,
            Direction.UP, n => { mCurrentDirection = Vector2.up; });
        mFMSLite.AddTranslation(Direction.UP, Direction.RIGHT,
            Direction.UP, n => { mCurrentDirection = Vector2.up; });
        mFMSLite.AddTranslation(Direction.DOWN, Direction.LEFT,
            Direction.DOWN, n => { mCurrentDirection = Vector2.down; });
        mFMSLite.AddTranslation(Direction.DOWN, Direction.RIGHT,
            Direction.DOWN, n => { mCurrentDirection = Vector2.down; });
        mFMSLite.AddTranslation(Direction.RIGHT, Direction.UP,
            Direction.RIGHT, n => { mCurrentDirection = Vector2.right; });
        mFMSLite.AddTranslation(Direction.RIGHT, Direction.DOWN,
            Direction.RIGHT, n => { mCurrentDirection = Vector2.right; });
        mFMSLite.AddTranslation(Direction.LEFT, Direction.UP,
            Direction.LEFT, n => { mCurrentDirection = Vector2.left; });
        mFMSLite.AddTranslation(Direction.LEFT, Direction.DOWN,
            Direction.LEFT, n => { mCurrentDirection = Vector2.left; });
        mFMSLite.Start(Direction.UP);
    }

    public void StartButtonTapped()
    {
        isStart = true;
        StartCoroutine(StartGame());
        foodCreater.CreateFoodPosition(mBody);
    }

    private IEnumerator StartGame()
    {
        while (isStart)
        {
            Move();
            yield return new WaitForSeconds(0.2f);
        }
    }

    private bool isStart = false;
    private bool mAte = false;
    private void Move()
    {
        var currentHead = transform.position;
        transform.Translate(mCurrentDirection);
        if (mAte)
        {
            var newNode = Instantiate(node,currentHead, Quaternion.identity);
            mBody.Insert(0, newNode);
            mAte = false;
            foodCreater.CreateFoodPosition(mBody);
        }
        else
        {
            if (mBody.Count == 0) return;
            var tail = mBody[mBody.Count - 1];
            tail.transform.position = currentHead;
            mBody.RemoveAt(mBody.Count - 1);
            mBody.Insert(0,tail);
        }
    }

    private  void OnTriggerEnter2D(Collider2D collider2D)
    {
        Debug.Log("显示：OnTriggerEnter2D" + collider2D.tag);
        if (collider2D.tag == "Food")
        {
            mAte = true;
            Destroy(collider2D.gameObject);
        }else if (collider2D.tag == "Floor")
        {
            isStart = false;
            Debug.Log("游戏结束");
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            mFMSLite.Trigger(Direction.UP);
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            mFMSLite.Trigger(Direction.LEFT);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            mFMSLite.Trigger(Direction.DOWN);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            mFMSLite.Trigger(Direction.RIGHT);
        }
    }
}
